<!--此页面对应排程任务管理的新增弹窗页面，http://localhost:8001/#/model-dataapstask-->
<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="任务编号" prop="cid">
      <el-input v-model="dataForm.cid" placeholder="任务编号"></el-input>
    </el-form-item>
    <el-form-item label="生成时间" prop="dtime">
      <el-input v-model="dataForm.dtime" :placeholder="dataForm.dtime" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="任务名称" prop="cname">
      <el-input v-model="dataForm.cname" placeholder="任务名称"></el-input>
    </el-form-item>
    <el-form-item label="任务描述" prop="cdesc">
      <el-input v-model="dataForm.cdesc" placeholder="任务描述"></el-input>
    </el-form-item>
    <el-form-item label="分类" prop="ctype">
      <el-input v-model="dataForm.ctype" placeholder="分类"></el-input>
    </el-form-item>
    <el-form-item label="模型Id" prop="capsjson">
      <el-input v-model="dataForm.capsjson" placeholder="模型Id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          cid: '',
          dtime: this.getNowFormatDate(),
          cname: '',
          cdesc: '',
          ctype: '',
          capsjson: ''
        },
        dataRule: {
          cid: [
            { required: true, message: '任务编号不能为空', trigger: 'blur' }
          ],
          cname: [
            { required: true, message: '任务名称不能为空', trigger: 'blur' }
          ],
          cdesc: [
            { required: true, message: '任务描述不能为空', trigger: 'blur' }
          ],
          ctype: [
            { required: true, message: '分类不能为空', trigger: 'blur' }
          ],
          capsjson: [
            { required: true, message: '模型位置不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/model/dataapstask/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.cid = data.dataApstask.cid
                this.dataForm.dtime = data.dataApstask.dtime
                this.dataForm.cname = data.dataApstask.cname
                this.dataForm.cdesc = data.dataApstask.cdesc
                this.dataForm.ctype = data.dataApstask.ctype
                this.dataForm.capsjson = data.dataApstask.capsjson
              }
            })
          }
        })
      },
      getNowFormatDate () {
        // 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子：
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
        // eslint-disable-next-line no-extend-native
        Date.prototype.Format = function (fmt) { // author: meizz
          var o = {
            'M+': this.getMonth() + 1, // 月份
            'd+': this.getDate(), // 日
            'h+': this.getHours(), // 小时
            'm+': this.getMinutes(), // 分
            's+': this.getSeconds(), // 秒
            'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
            'S': this.getMilliseconds() // 毫秒
          }
          if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length)) }
          for (var k in o) { if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length))) }
          return fmt
        }

        return new Date().Format('yyyy-MM-dd hh:mm:ss')
      },

      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/model/dataapstask/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'cid': this.dataForm.cid,
                'dtime': this.dataForm.dtime,
                'cname': this.dataForm.cname,
                'cdesc': this.dataForm.cdesc,
                'ctype': this.dataForm.ctype,
                'capsjson': this.dataForm.capsjson
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
